package com.ourstocks.module.maindata.services;

import com.alibaba.fastjson.JSONArray;
import com.ourstocks.common.util.stock.impl.TushareSpider;
import com.ourstocks.module.maindata.po.StockCompany;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 1812T4
 * @create 2018-10-31 16:42
 **/
@Service
public class StockCompanyService {

	Logger log = LoggerFactory.getLogger(StockCompanyService.class);
	@Autowired
	private TushareSpider tushareSpider;
	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 代码列表刷新
	 */
	public void refreshStockCompany() {
		List<StockCompany> list = new ArrayList<StockCompany>();
		JSONArray rows_sh = tushareSpider.getStockShCompany();
		JSONArray rows_sz = tushareSpider.getStockSZCompany();
		if (rows_sh != null) {// 排除空指针的可能，add by chuan'an 2019.5.1
			for (int i = 0; i < rows_sh.size(); i++) {
				list.add(new StockCompany(rows_sh.getJSONArray(i)));
			}
		}
		if (rows_sz != null) {// 排除空指针的可能，add by chuan'an 2019.5.1
			for (int i = 0; i < rows_sz.size(); i++) {
				list.add(new StockCompany(rows_sz.getJSONArray(i)));
			}
		}
		// 先删除再新增
		mongoTemplate.dropCollection(StockCompany.class);
		mongoTemplate.insertAll(list);
	}
}
